package com.ruoyi.production.service.impl.seed;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.production.domain.seed.SeedAmount;
import com.ruoyi.production.domain.seed.SeedPurchase;
import com.ruoyi.production.mapper.seed.SeedAmountMapper;
import com.ruoyi.production.mapper.seed.SeedPurchaseMapper;
import com.ruoyi.production.service.seed.ISeedPurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 种子购买信息Service业务层处理
 *
 * @author ruoyi
 * @date 2021-07-07
 */
@Service
public class SeedPurchaseServiceImpl implements ISeedPurchaseService {
    @Autowired
    private SeedPurchaseMapper seedPurchaseMapper;
    @Autowired
    private SeedAmountMapper seedAmountMapper;

    /**
     * 查询种子购买信息
     *
     * @param seedPurchaseId 种子购买信息ID
     * @return 种子购买信息
     */
    @Override
    public SeedPurchase selectSeedPurchaseById(Long seedPurchaseId) {
        return seedPurchaseMapper.selectSeedPurchaseById(seedPurchaseId);
    }

    /**
     * 查询种子购买信息列表
     *
     * @param seedPurchase 种子购买信息
     * @return 种子购买信息
     */
    @Override
    public List<SeedPurchase> selectSeedPurchaseList(SeedPurchase seedPurchase) {
        return seedPurchaseMapper.selectSeedPurchaseList(seedPurchase);
    }

    /**
     * 新增种子购买信息
     *
     * @param seedPurchase 种子购买信息
     * @return 结果
     */
    @Override
    public int insertSeedPurchase(SeedPurchase seedPurchase) {
        seedPurchase.setCreateTime(DateUtils.getNowDate());
        return seedPurchaseMapper.insertSeedPurchase(seedPurchase);
    }

    /**
     * 修改种子购买信息
     *
     * @param seedPurchase 种子购买信息
     * @return 结果
     */
    @Override
    public int updateSeedPurchase(SeedPurchase seedPurchase) {
        seedPurchase.setUpdateTime(DateUtils.getNowDate());
        return seedPurchaseMapper.updateSeedPurchase(seedPurchase);
    }

    /**
     * 批量删除种子购买信息
     *
     * @param seedPurchaseIds 需要删除的种子购买信息ID
     * @return 结果
     */
    @Override
    public int deleteSeedPurchaseByIds(Long[] seedPurchaseIds) {
        return seedPurchaseMapper.deleteSeedPurchaseByIds(seedPurchaseIds);
    }

    /**
     * 删除种子购买信息信息
     *
     * @param seedPurchaseId 种子购买信息ID
     * @return 结果
     */
    @Override
    public int deleteSeedPurchaseById(Long seedPurchaseId) {
        return seedPurchaseMapper.deleteSeedPurchaseById(seedPurchaseId);
    }

    /**
     * 插入种子购买表更新种子量表
     *
     * @param seedPurchase
     * @return
     */
    @Override
    public int insertPurchaseUpdateAmount(SeedPurchase seedPurchase) {
        SeedAmount seedAmount = new SeedAmount();
        seedAmount.setSeedType(seedPurchase.getSeedType());
        seedAmount.setCreateTime(seedPurchase.getCreateTime());
        seedAmount.setCreateBy(seedPurchase.getCreateBy());
        seedAmount.setUpdateTime(seedPurchase.getUpdateTime());
        seedAmount.setUpdateBy(seedPurchase.getUpdateBy());
        seedAmount.setVarietyName(seedPurchase.getVarietyName());
        seedAmount.setCropTypeId(seedPurchase.getCropTypeId());
        seedAmount.setOriginCompany(seedPurchase.getOriginCompany());
        List<SeedAmount> seedAmounts = seedAmountMapper.selectSeedAmountList(seedAmount);
        seedAmount.setStorageAmount(seedPurchase.getPurchaseAmount());
        if (!StringUtils.isEmpty(seedAmounts)){
            SeedAmount amount = seedAmounts.get(0);
            amount.setStorageAmount(amount.getStorageAmount()+seedPurchase.getPurchaseAmount());
            seedAmountMapper.updateSeedAmount(amount);
        }else {
            seedAmountMapper.insertSeedAmount(seedAmount);
        }
        return insertSeedPurchase(seedPurchase);
    }
}
